/*
 *	Copyright © 2008 University of Houston
 *	All rights reserved.
 */

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include "session.h"
#include "analyzer.h"
#include "logger.h"
// #include "logplotter.h"
// #include "results-filter.h"

using namespace std;

#define DEFAULT_SESS_NUM	"0"
#define DEFAULT_FILTER		"0"
#define DEFAULT_ALG			"0"
#define DEFAULT_RTT			"-1"

void displayUsage(char **argv);
bool getParams(int argc, char** argv, string *session, string *sessFilter, string *alg, string *rtt, string *logfile);

int main(int argc, char **argv)
{
	// Display licensing agreement.
	cout << "*	Copyright © 2008 University of Houston\n"
	"*	All rights reserved." << endl;

	// Get all of our parameters
	string logfile	= DEFAULT_LOGFILE;
	string filter	= DEFAULT_FILTER;
	string alg		= DEFAULT_ALG;
	string sess		= DEFAULT_SESS_NUM;
	string rtt		= DEFAULT_RTT;
	if ( !getParams(argc, argv, &sess, &filter, &alg, &rtt, &logfile) )
		return -1;
	int sessNum 	= strtol(sess.c_str());
	int filterID	= strtol(filter.c_str());
	int algID		= strtol(alg.c_str());
	//long double expRtt 		= strtold(rtt.c_str(), NULL);

	// Create the session object, and set the analyzer
	session 	s;

	// Load the session from the log file.
	packetLogger logger;
	if ( !logger.loadSessionFromLog(logfile, &s, sessNum))
	{
		ERROR("Could not perform analysis on session " << sessNum
				<< " in log %s." << logfile);
		return -1;
	}

	// Perform the analysis
	sessionAnalyzer *pAnalyzer = new sessionAnalyzer();
	s.setAnalyzer(pAnalyzer);
	pAnalyzer->simulate(&s, filter::getFilter(filterID), algorithm::getAlg(algID),
			sessNum, logfile);

	// // Make a copy of the results and sort the copy
	// resultsFilter *resultFilter 		= new sortInOrderFilter;
	// analyzerResultList sortedResults 	= (*resultFilter)(*(pAnalyzer->getResultList()));
	// cout << "Results has " << sortedResults.size() << " elements" << endl;


	// // Create our plotter, plot the results
	// pointPlotter plotter;
	// plotter.plot<long double>(&sortedResults, "black", expRtt);
	// plotter.labelXAxis("Ordered numbers-->");
	// plotter.labelYAxis("Number Value-->");

	// Success
	return 0;
}

/*
 * Checks the command-line arguments, and parses them into distinct commands.
 * @param argc 		[argc] as provided on the command-line.
 * @param argv 		[argv] as provided on the command-line.
 * @param session 	Pointer to a string to store the session number.
 * @param sessFilt	Pointer to a string to store the session filter.
 * @param alg 		Pointer to a string to store the algorith number.
 * @param rtt 		Pointer to a string to store the expected plot value.
 * @param logfile 	Pointer to a string to store the log file.
 * @return 			False if there were not enough parameters provided.
 */
bool getParams(int argc, char** argv, string *session, string *sessFilter, string *alg, string *rtt, string *logfile)
{
	int index;
	int c;
	opterr = 0;

	while ((c = getopt (argc, argv, "s:f:l:a:r:h?")) != -1)
		switch (c)
		{
		case 'l':
			*logfile = optarg;
			break;
		case 'f':
			*sessFilter = optarg;
			break;
		case 's':
			*session = optarg;
			break;
		case 'a':
			*alg = optarg;
			break;
		case 'r':
			*rtt = optarg;
			break;
		case 'h':
		case '?':
			displayUsage(argv);
			return false;
		default:
			ERROR("Option \'-" << optopt << "\' not supported.\n")
			break;
		}

	for (index = optind; index < argc; index++)
		ERROR ("Non-option argument " << argv[index])

	return true;
}

/*
 * Display the usage
 */
void displayUsage(char **argv)
{

	printf("Usage: %s [args]\n"
		   "Supported Arguments: \n"
		   "-a n       Log analyzer to use.  Default \'%s\'\n"
		   "-f n       Session filter to use.  Default \'%s\'\n"
		   "-l path    Path the the logfile.  Default \'%s\'\n"
		   "-s n       Session number to load. Default \'%s\'\n"
		   "-r n       Expected Plot Value. Default \'%s\'\n",
		   argv[0], DEFAULT_ALG, DEFAULT_FILTER, DEFAULT_LOGFILE, DEFAULT_SESS_NUM, DEFAULT_RTT);
}

